﻿Imports System.Data.SqlClient
Public Class en_members_productsubmitpic
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim productId, strErr, id, path, photoType, fileFullName, fileName, pId As String
        strErr = ""
        If (Session("uid") <> Nothing And Session("uid") <> "") Then
            If (Request("productId") <> Nothing And Request("productId") <> "") Then
                If (Request("submitproductimage") <> Nothing And Request("submitproductimage") <> "") Then
                    productId = Request("productId")
                    Dim i As Integer
                    For i = 0 To Request.Files.Count - 1
                        Dim myFile As HttpPostedFile = Request.Files(i)
                        If (myFile.ContentLength <> 0) Then
                            pId = System.Guid.NewGuid().ToString()
                            fileFullName = myFile.FileName
                            fileName = fileFullName.Substring(fileFullName.LastIndexOf(".") + 1)
                            System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/product/" & productId)
                            myFile.SaveAs(Server.MapPath("../../") & "pic/product/" & productId & "/" & pId & "." & fileName)
                            path = "/pic/product/" & productId & "/" & pId & "." & fileName
                            id = System.Guid.NewGuid().ToString()
                            photoType = myFile.FileName.Substring(myFile.FileName.LastIndexOf(".") + 1)
                            ps.strSql = "insert into product_photo(path,product_id,photo_type) values(@path,@productId,@photoType)"
                            ps.parameters.Clear()
                            ps.addParameter("path", path)
                            ps.addParameter("productId", productId)
                            ps.addParameter("photoType", True)
                            objfind.es(ps, strErr)
                        End If
                    Next
                    Response.Redirect("members-supplyManagement.aspx")
                End If
            End If

        Else
            Response.Redirect("../login.aspx")
        End If
    End Sub

End Class